package com.fandou.paiti.topic

import android.content.Context
import android.content.Intent
import android.content.res.Configuration
import android.os.Bundle
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.viewpager.widget.ViewPager
import com.abcpen.model.AnswerModel
import com.blankj.utilcode.util.BarUtils
import com.blankj.utilcode.util.LogUtils
import com.bumptech.glide.Glide
import com.fandou.paiti.R
import com.flyco.tablayout.SegmentTabLayout
import com.flyco.tablayout.listener.OnTabSelectListener
import com.google.gson.Gson
import java.util.*

/**
 * @author       Jm
 * @date         2020/6/2215:58
 * @description  the topic search result
 */
class TopicResultActivity : AppCompatActivity(), ViewPager.OnPageChangeListener, OnTabSelectListener {

    private var mImageUrl: String? = null

    private var mAnswerList: ArrayList<AnswerModel>? = null

    private var mImageId: String? = null

    private var mStb: SegmentTabLayout? = null

    private var mViewPager: ViewPager? = null

    /**
     * 标识当前的页码
     */
    private var mCurrentPageIndex = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
//        BarUtils.setStatusBarColor(this, ContextCompat.getColor(this, R.color.colorPrimary))
//        BarUtils.setStatusBarLightMode(this, true)

        BarUtils.transparentStatusBar(this)
        BarUtils.setStatusBarLightMode(this, true)
        setContentView(R.layout.topic_result_activity)

        mImageUrl = intent.getStringExtra(INTENT_IMAGE_URL)
        mAnswerList = intent.getParcelableArrayListExtra(INTENT_CONTENT)
        mImageId = intent.getStringExtra(INTENT_IMAGE_ID)
        initView()
        initData()
    }


    private fun initView() {
        mStb = findViewById(R.id.stb)
        mViewPager = findViewById(R.id.view_pager)
        mStb!!.setOnTabSelectListener(this)
        mViewPager!!.addOnPageChangeListener(this)
    }


    private fun initData() {
        val ivTopic = findViewById<ImageView>(R.id.ivTopic)
        mImageUrl?.let {
            Glide.with(this).load(mImageUrl).into(ivTopic)
        }

        val fragments = ArrayList<Fragment>()

        val mTitles = arrayListOf<String>()
        mAnswerList!!.forEachIndexed { index, answerModel ->
            fragments.add(TopicResultFragment.newInstance(mAnswerList!!.get(index)))
            mTitles.add("题目${getTopicNumber(index + 1)}")
        }

        mStb!!.setTabData(mTitles.toTypedArray())

        mViewPager!!.adapter = BaseFragmentStateAdapter(fragments, supportFragmentManager)
        mViewPager!!.offscreenPageLimit = fragments.size
        mViewPager!!.currentItem = mCurrentPageIndex
        mStb!!.currentTab = mCurrentPageIndex
    }

    /**
     * 获得中文题号
     */
    private fun getTopicNumber(number: Int): String {
        when (number) {
            1 -> {
                return "一"
            }

            2 -> {
                return "二"
            }

            3 -> {
                return "三"
            }
            else -> {
                return ""
            }
        }
    }

    override fun onConfigurationChanged(newConfig: Configuration) {
        super.onConfigurationChanged(newConfig)
        if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
            // land do nothing is ok
            setContentView(R.layout.topic_result_activity)

        } else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
            // port do nothing is ok
            setContentView(R.layout.topic_result_activity)
        }

        mImageUrl = intent.getStringExtra(INTENT_IMAGE_URL)
        mAnswerList = intent.getParcelableArrayListExtra(INTENT_CONTENT)
        mImageId = intent.getStringExtra(INTENT_IMAGE_ID)
        LogUtils.d("mAnswerList:${Gson().toJson(mAnswerList)}")
        initView()
        initData()
    }

    override fun onPageScrollStateChanged(position: Int) {
    }

    override fun onPageScrolled(p0: Int, p1: Float, p2: Int) {
    }

    override fun onPageSelected(position: Int) {
        mStb!!.currentTab = position
        mCurrentPageIndex = position
    }

    override fun onTabSelect(position: Int) {
        mCurrentPageIndex = position
        mViewPager?.currentItem = position
    }

    override fun onTabReselect(position: Int) {
    }

    override fun onDestroy() {
        super.onDestroy()
        mViewPager?.removeOnPageChangeListener(this)
    }


    companion object {
        const val INTENT_IMAGE_URL = "intent_image_url"

        const val INTENT_CONTENT = "intent_content"

        const val INTENT_IMAGE_ID = "intent_image_id"

        fun start(
            context: Context, imageUrl: String?, imageid: String?,
            data: ArrayList<AnswerModel?>?
        ) {
            val intent = Intent(context, TopicResultActivity::class.java)
            intent.putExtra(INTENT_IMAGE_URL, imageUrl)
            intent.putExtra(INTENT_CONTENT, data)
            intent.putExtra(INTENT_IMAGE_ID, imageid)
            context.startActivity(intent)
        }
    }


}